package eu.boss.activities;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

import eu.boss.R;
import eu.boss.model.Player;
import eu.boss.model.ShadowColor;

/**
 * This activity is displaying to add a new player.
 * 
 * @author Arnaud
 * 
 */

public class NewPlayerActivity extends SherlockActivity {
	
	private List<String> colorList;
	private ShadowColor colorSelected;
	private Player p;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newplayer);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		// initialize components
		createColorList();
		final EditText edtNewPlayer = (EditText) findViewById(R.id.etNewPlayerName);
		Spinner spinner = (Spinner) findViewById(R.id.spinnerColor);
		Button btnConfirm = (Button) findViewById(R.id.btnConfirm);

		// spinner adapter which will add datas in the spinner
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, android.R.id.text1);
		spinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(spinnerAdapter);

		// adding datas in the adapter
		for (int i = 0; i < colorList.size(); i++) {
			spinnerAdapter.add(colorList.get(i));
		}
		// update UI
		spinnerAdapter.notifyDataSetChanged();

		// putting a listener on the two buttons
		btnConfirm.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (v.getId() == R.id.btnConfirm) {
					if (edtNewPlayer.getText().toString().compareTo("") != 0) {
						p = new Player(edtNewPlayer.getText().toString(),
								colorSelected, 0, 0, 0, 0, 0, 0);
						try {

							ObjectMapper mapper = new ObjectMapper();
							String strPlayer;
							strPlayer = mapper.writeValueAsString(p);
							Intent intent = new Intent();
							intent.putExtra("player", strPlayer);
							setResult(RESULT_OK, intent);
							finish();
						} catch (JsonGenerationException e) {
							e.printStackTrace();
						} catch (JsonMappingException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						}
					} else
					// nothing in the editText so displaying an error message
					{
						AlertDialog.Builder adb = new AlertDialog.Builder(
								NewPlayerActivity.this);
						adb.setTitle("Erreur");
						adb.setMessage("Veuillez entrer un nom svp");
						adb.setPositiveButton("Ok", null);
						adb.show();
					}
				}
			}
		});

		// putting a listener on the spinner. The color is changed with the
		// selected item
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View v,
					int position, long id) {
				switch (position) {
				case 0:
					colorSelected = ShadowColor.WHITE;
					break;
				case 1:
					colorSelected = ShadowColor.BLUE;
					break;
				case 2:
					colorSelected = ShadowColor.GRAY;
					break;
				case 3:
					colorSelected = ShadowColor.YELLOW;
					break;
				case 4:
					colorSelected = ShadowColor.BLACK;
					break;
				case 5:
					colorSelected = ShadowColor.ORANGE;
					break;
				case 6:
					colorSelected = ShadowColor.PINK;
					break;
				case 7:
					colorSelected = ShadowColor.RED;
					break;
				case 8:
					colorSelected = ShadowColor.SAND;
					break;
				case 9:
					colorSelected = ShadowColor.GREEN;
					break;
				case 10:
					colorSelected = ShadowColor.PURPLE;
					break;
				default:
					break;
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
			// Do nothing
		});
	}

	
	/**
	 * Callback method when an actionbar's option item is selected 
	 */
	  @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        //This uses the imported MenuItem from ActionBarSherlock	  
		  	if (item.getTitle().toString().compareTo(getTitle().toString())==0)
			  	finish();
	        return true;
	    }
	  
	  
	/**
	 * Creation of the colorList
	 */
	public void createColorList() {
		colorList = new ArrayList<String>();
		colorList.add(getString(R.string.white));
		colorList.add(getString(R.string.blue));
		colorList.add(getString(R.string.gray));
		colorList.add(getString(R.string.yellow));
		colorList.add(getString(R.string.black));
		colorList.add(getString(R.string.orange));
		colorList.add(getString(R.string.pink));
		colorList.add(getString(R.string.red));
		colorList.add(getString(R.string.sand));
		colorList.add(getString(R.string.green));
		colorList.add(getString(R.string.purple));
		
	}

}
